package com.cskaoyan.controller;


import com.cskaoyan.aspect.OperationLog;
import com.cskaoyan.bean.vo.AdminAdForListDataBean;
import com.cskaoyan.bean.vo.AdminAdForListItemBean;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.service.AdminAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
@RequestMapping("admin/ad")
public class AdminAdController {
    @Autowired
    AdminAdService adminAdService;
    @RequestMapping("list")
    public BaseRespVo list(Integer page, Integer limit, String name, String content, String sort, String order){
        AdminAdForListDataBean<AdminAdForListItemBean> dataBean = adminAdService.queryAdByNameAndContent(page, limit, name, content, sort, order);
        return BaseRespVo.ok(dataBean);
    }

    @RequestMapping("create")
    public BaseRespVo create(@RequestBody AdminAdForListItemBean adBean){
        Integer result = adminAdService.insertAd(adBean);
        return BaseRespVo.ok(adBean);
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody AdminAdForListItemBean adBean){
        Integer result = adminAdService.updateAdByAdminAdForListItemBean(adBean);
        if (result == 404) {
            return BaseRespVo.fail();
        }
        AdminAdForListItemBean consequence = adminAdService.getAdminAdForListItemBeanById(adBean.getId());

        return BaseRespVo.ok(consequence);
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody AdminAdForListItemBean adBean){
        Integer result = adminAdService.deleteAdByAdminAdForListItemBean(adBean);
        return result == 1 ? BaseRespVo.ok():BaseRespVo.fail();
    }
}
